<?php
/**
 * class App_Controller_Profile
 *
 * @author kim
 */
class App_Controller_Profile extends Zend_Controller_Action
{
    /**
     * init function
     */
    public function init()
    {
    }

    /**
     * default action
     */
    public function indexAction()
    {
        if (false == App_Model_User::isLogedIn()) {
            $this->_helper->redirector('login', 'user');
        }

        $form = new App_Form_Profile_View();

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            // validate post data
            if ($form->isValid($data)) {
                $user = new App_Model_User();
                $result = $user->getProfileUpdateResult($form->getValues());
                if (true === $result) {
                    $this->_helper->redirector('index');
                }
            } else {
                $form->populate($data);
            }
        }

        $profile = Zend_Registry::get('user');
        $form->setUserDefaults($profile);

        $review = new App_Model_Review();
        $reviews = $review->getReviewsByUserId($profile['id']);

        $this->view->profile = $profile;
        $this->view->formProfile = $form;
        $this->view->reviews = $reviews;
    }

    /**
     * view user profile
     */
    public function viewAction()
    {
        $userId = $this->getRequest()->getParam('user_id');
        if ($userId) {
            $user = new App_Model_User();
            $this->view->profile = $user->getProfile($userId);
        }
    }

}
